package com.zycx;

import com.zycx.interceptor.AuthInterceptor;
import com.zycx.interceptor.CurrentUserMethodArgumentResolver;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.List;


/**
 * Description:  拦截器
 * Created by zycx on 2018-12-05 23:34
 * Created with IntelliJ IDEA.
 */
@Slf4j
@Configuration
public class WebMvcConfigurer extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.info("配置静态资源访问路径 ......");
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");
    }

    //关键，将拦截器作为bean写入配置中
    @Bean
    public AuthInterceptor getSecurityInterceptor() {
        return new AuthInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        log.info("正在配置拦截器 ......");
        // 注册拦截器
        InterceptorRegistration ir = registry.addInterceptor(getSecurityInterceptor());
        // 配置拦截的路径
        ir.addPathPatterns("/api/**");
        // 还可以在这里注册其它的拦截器
        // registry.addInterceptor(new AppAuthInterceptor()).addPathPatterns("/api/**");
        // 配置不拦截的路径
        ir.excludePathPatterns("**/main/");
    }

    // ==============  通过注解获取当前登录用户信息配置  start  ======================================================================//
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(currentUserMethodArgumentResolver());
        super.addArgumentResolvers(argumentResolvers);
    }
    @Bean
    public CurrentUserMethodArgumentResolver currentUserMethodArgumentResolver() {
        return new CurrentUserMethodArgumentResolver();
    }
// ==============  通过注解获取当前登录用户信息配置  end  ======================================================================//


}
